package com.fenghongzhang.module_home.splash

import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.fenghongzhang.lib_common.activity.CommonWebActivity
import com.fenghongzhang.lib_common.service.HelloService
import com.fenghongzhang.lib_common.utils.ARouterUtils
import com.fenghongzhang.lib_common.utils.Router
import com.fenghongzhang.lib_core.base.BaseActivity
import com.fenghongzhang.module_home.R
import com.fenghongzhang.module_home.databinding.ActivitySplashBinding
import com.fenghongzhang.module_home.dialog.MessageNoticeDialog
import com.fenghongzhang.module_home.getAttrValue
import com.jczy.cyclone.mmkv.AppMMKV
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class SplashActivity :  BaseActivity<SplashViewModel, ActivitySplashBinding>(){

    override val mViewModel: SplashViewModel by viewModels()
    override val mLayoutResId: Int = R.layout.activity_splash
     @Autowired(name = "/mine/hello")
    lateinit  var helloService2: HelloService


    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this);

    }

    override fun initListener() {

    }

    override fun initData() {
        println("nihao------")
        val sayHello = helloService2.sayHello("hahahahahahah-----")
        println("helloService2------$sayHello")

        //第一次
        if (!AppMMKV.splashAgreement) {//get
            //取个颜色
            val colorMain = getAttrValue(this, R.attr.colorTheme)
            val userAgreement = "《用户协议》"
            val privacyPolicy = "《隐私条款》"
            val content =
                "宗申骑士俱乐部非常尊重用户个人的信息保护，我们依据最新的监管要求特向您说明如下：\n1.在使用 APP过程中，我们有可能会收集、使用设备标识信息用于推送车辆提醒消息。\n2.我们会一直采取严格的安全技术保护您的个人信息安全。\n3.未经您的同意，我们不会以协议约定之外的形式和渠道获取、共享或使用您的个人信息。\n您可通过阅读完整的${userAgreement}和${privacyPolicy}来了解详细信息。请您充分阅读并理解以上协议，点击同意按钮，即表示您已同意上述协议及以上约定。"
            val spannableStringBuilder = SpannableStringBuilder(content)
            val userAgreementStart = content.indexOf(userAgreement)
            val userAgreementEnd = userAgreementStart + userAgreement.length
            val privacyPolicyStart = content.indexOf(privacyPolicy)
            val privacyPolicyEnd = privacyPolicyStart + privacyPolicy.length

            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
                    CommonWebActivity.jumpUserAgreement()
                }

                override fun updateDrawState(ds: TextPaint) {
                    //ds.color = ContextCompat.getColor(this@SplashActivity, colorMain)
                   // ds.isUnderlineText = false
                }

            }, userAgreementStart, userAgreementEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
                   // CommonWebActivity.jumpPrivacyAgreement()
                }

                override fun updateDrawState(ds: TextPaint) {
                   // ds.color = ContextCompat.getColor(this@SplashActivity, colorMain)
                   // ds.isUnderlineText = false
                }

            }, privacyPolicyStart, privacyPolicyEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            MessageNoticeDialog(
                title = "温馨提示",
                message = spannableStringBuilder,
                cancelText = "不同意",
                confirmText = "同意",
                alignment = Gravity.START,
                onConfirmClick = {
                    //同意隐私政策
                    AppMMKV.splashAgreement = true
                    //记录当前版本号
                  //  AppMMKV.lastVersionCode = getVersionCode(this)
                   // mHandler.sendEmptyMessageDelayed(2, SPLASH_MAX_DELAY)
                },
                onCancelClick = { finish() }
            ).show(supportFragmentManager)
          //  AppMMKV.splashAgreement = true
        }
        ARouterUtils.jump(Router.Ui.HomeActivity)
    }
}
